﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class PavilionDao
    {


        public PavilionDao()
        {

            Mapper.CreateMap<E_Pavilion, PavilionModel>();
        }
        public List<E_Pavilion> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_Pavilion.Count();

                return edm.E_Pavilion.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public PavilionModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Pavilion.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_Pavilion, PavilionModel>(query);
            }

        }


        public int Save(PavilionModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Pavilion.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_Pavilion();

                    edm.AddToE_Pavilion(query);

                }


                query.PaName = obj.PaName;

                query.PaLength = obj.PaLength;

                query.PaWidth = obj.PaWidth;

                query.OrderID = obj.OrderID;
                query.SiteID = obj.SiteID;
                query.CreateUserId = obj.CreateUserId;
                edm.SaveChanges();

                return query.Id;


            }

        }

        public List<PavilionModel> GetPavilionList(int stid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_Pavilion>, List<PavilionModel>>(edm.E_Pavilion.Where(s => s.SiteID == stid).ToList());
            }
        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_Pavilion.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

